<!DOCTYPE html>
<html lang='zh-CN'>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

  <title>问答中心管理</title>
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link href="css/materialdesignicons.min.css" rel="stylesheet">
  <link href="css/animate.css" rel="stylesheet">
  <link href="css/style.min.css" rel="stylesheet">
  <link href="js/select2/select2.min.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid p-t-15">

  <div class="row">
    <div class="col-md-12">
      <div class="card">
        <div class="card-header"><h4>问答中心</h4></div>
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane fade active in" id="home">

              <form class="form-inline" method="post" id="searchForm">
                <div class="form-group">
                  <label>问题标题:</label>
                  <input type="text" class="form-control" name="title">
                </div>
                <div class="form-group">
                  <button class="btn btn-info btn_query btn-round"  type="button" id="searchFormBtn" >查询</button>
                </div>
                <div class="form-group">
                  <button class="btn btn-success btn_query btn-round" type="button" id="addBtn" >添加</button>
                </div>
                <div class="form-group">
                  <button class="btn btn-warning btn_query btn-round" type="button" id="editBtn">编辑</button>
                </div>
                <div class="form-group">
                  <button class="btn btn-danger btn_query btn-round" type="button" id="deleteBtn">删除</button>
                </div>
              </form>
              <table id="qa_data"></table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!--  添加与编辑模态框 -->
  <div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" >
    <div class="modal-dialog modal-lg" role="document" style="width: 90%">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="exampleModalLabel">问答中心管理</h4>
        </div>
        <div class="modal-body">
          <form id="addOrEditForm"  method="post">
            <input type="hidden" name="id">
            <div class="form-group">
              <label for="recipient-proName" class="control-label">问题：</label>
              <input type="text" class="form-control" id="recipient-proName" name="title">
            </div>
            <div class="row">
              <div class="col-xs-12">
                <div class="input-group m-b-10">
                  <label for="editor" class="control-label">回答内容：</label>
                  <textarea id="editor" name="content"></textarea>
                </div>
              </div>
            </div>
          </form>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" id="saveFormBtn" class="btn btn-primary">保存</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-table/bootstrap-table.js"></script>
<script type="text/javascript" src="js/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
<script type="text/javascript" src="js/zero2one/zero2one.version.js"></script>
<script type="text/javascript" src="js/zero2one/zero2one.core.js"></script>
<script type="text/javascript" src="js/dropzone/min/dropzone.min.js"></script>
<script type="text/javascript" src="js/bootstrapvalidator/dist/js/bootstrapValidator.js"></script>
<!-- bootstrap表单验证插件 -->
<script type="text/javascript" src="js/bootstrap-validator-master/dist/validator.js"></script>
<!-- jquery-form表单提交插件 -->
<script type="text/javascript" src="js/jquery-form/jquery.form.js"></script>
<!-- bootstrap弹出框插件 -->
<script type="text/javascript" src="js/jquery.bootstrap.min.js"></script>
<!-- 表单填充插件 -->
<script type="text/javascript" src="js/jquery-form/jquery.formloader.js"></script>
<!-- ckeditor富文本插件 -->
<script src="js/ckeditor/ckeditor.js"></script>

<script type="text/javascript">

  //设置表头
  var columns = [
    {checkbox: true},
    {field: 'id',title: 'ID'},
    {field: 'title',title: '问题'},
    {field: 'content',title: '回答内容'},
    {field: 'operatorName',title: '操作人名称'},
    {field: 'operatorTime',title: '操作时间',formatter: function (value) {
            return zero2one.dateFormat(value, 'Y-m-d H:i:s')}}
  ];

  var ck = CKEDITOR.replace( 'editor' );

  //进入主界面加载数据
  zero2one.datagrid("qa_data", "/remote/memberQa/list",function (params) {
    return $.extend(zero2one.tableInitParam(params), zero2one.form2json("searchForm"));
  }, columns);

  zero2one.girdClick("qa_data");

  //点击查询按钮重新加载页面数据
  $("#searchFormBtn").click(function () {
    $('#qa_data').bootstrapTable('refresh');
  });

  //点击添加按钮弹出模态框
  $("#addBtn").click(function () {
    ck.setData("");//ck清空
    $("#addOrEditForm")[0].reset();
    $("#addOrEditModal").modal("show");
  });

  //点击编辑按钮弹出模态框
  $("#editBtn").click(function () {
    //获取选中行的数据
    var datas = $('#qa_data').bootstrapTable('getSelections');
    console.log(datas.length);
    //判断是否选中行数据，如果没有则提示
    if(datas.length == 1){
      ck.setData(datas[0].content);//ck回显
        $("#editor").val(datas[0].content);
        console.log(ck);
      //填充表单
      $("#addOrEditForm").formloader({data: datas[0]});
    }else{
      $.messager.alert("温馨提示", "请选择一条数据操作");
      return;
    }
    $("#addOrEditModal").modal("show");
  });

  $('#addOrEditForm').bootstrapValidator({
    fields: {
      title: {validators: {notEmpty: {message: '标题不能为空'}}},
      content: {validators: {notEmpty: {message: '内容不能为空'}}},
    }
  });

  //点击删除按钮弹出模态框
  $("#deleteBtn").click(function () {
    //获取选中行的数据
    var datas = $('#qa_data').bootstrapTable('getSelections');
    //判断是否选中行数据，如果没有则提示
    if(datas.length > 0){
      $.messager.confirm("温馨提示", "您确定要删除选中的数据吗?", function () {
        //发送请求,执行批量删除
        zero2one.request("/remote/memberQa/deleteQa",{"id": zero2one.array2str(datas)},function (data) {
          window.location.reload();
        });
      })
    }else{
      $.messager.alert("温馨提示", "请选择一条或多条数据操作");

    }
  });

  //清除选中行
  function dji(tableId) {
    $('#'+tableId).bootstrapTable("uncheckAll");
  }

  //保存
  $("#saveFormBtn").click(function () {
    var bv = $('#addOrEditForm').data('bootstrapValidator');
    bv.validate();
    if (bv.isValid()) {
      //获取富文本编辑器的内容,设置到表单组件中
      var data = ck.getData();
      $("#editor").val(data);
      zero2one.request("/remote/memberQa/updateQa",$("#addOrEditForm").serialize(),function (data) {
        window.location.reload();
      });
    }
  });

</script>
</body>
</html>
